package com.hemaapp.yjnh.view;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.ShareActivity;
import com.umeng.socialize.Config;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import xtom.frame.util.XtomToastUtil;

/**
 * 类描述：分享dialog
 * 创建人：GuiyouZhang
 * 创建时间：2016/3/30 16:31
 * 修改备注：
 */
public class ShareDialog implements View.OnClickListener{

    private Activity activity;
    private Dialog dialog;
    private Display display;
    private String title, content, url;
    private UMImage image;

    private TextView tvWechat;
    private TextView tvWechatMoment;
    private TextView tvOther;
    private Button btnCancel;

    @Override
    public void onClick(View v) {
        if ( v == btnCancel ) {
            dialog.dismiss();
        }else if (v == tvWechat) {
            dialog.dismiss();
            new ShareAction(activity)
                    .setPlatform(SHARE_MEDIA.WEIXIN)
                    .setCallback(umShareListener)
//                    .withTitle(title)
                    .withTitle(content)
                    .withText(content)
                    .withTargetUrl(url)
                    .withMedia(image)
                    .share();
        }else if (v == tvWechatMoment) {
            dialog.dismiss();
            new ShareAction(activity)
                    .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                    .setCallback(umShareListener)
//                    .withTitle(title)
                    .withTitle(content)
                    .withText(content)
                    .withTargetUrl(url)
                    .withMedia(image)
                    .share();
        }else if (v == tvOther) {
            dialog.dismiss();
            Intent it = new Intent(activity, ShareActivity.class);
            it.putExtra("id", "0");
            activity.startActivity(it);
        }
    }

    public ShareDialog(Activity activity, String title, String content, String url, UMImage image) {
        this.activity = activity;
        WindowManager windowManager = (WindowManager) activity
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.title = title;
        this.content = content;
        this.url = url;
        this.image = image;
        ProgressDialog dialog =  new ProgressDialog(activity);
        dialog.setMessage("请稍候");
        Config.dialog = dialog;
    }

    public ShareDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_share, null);
        view.setMinimumWidth(display.getWidth());
        //初始化控件
        tvWechat = (TextView) view.findViewById( R.id.tv_wechat );
        tvWechatMoment = (TextView) view.findViewById( R.id.tv_wechat_moment );
        tvOther = (TextView) view.findViewById( R.id.tv_other );
        btnCancel = (Button) view.findViewById( R.id.btn_cancel );
        tvWechat.setOnClickListener(this);
        tvWechatMoment.setOnClickListener(this);
        tvOther.setOnClickListener(this);
        btnCancel.setOnClickListener( this );

        // 定义Dialog布局和参数
        dialog = new Dialog(activity, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            XtomToastUtil.showShortToast(activity, "分享成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            XtomToastUtil.showShortToast(activity, "分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            XtomToastUtil.showShortToast(activity, "取消了分享");
        }
    };

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }
}
